﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Calculator : Form
    {
        private decimal firstNum = decimal.MinValue;
        private decimal secondNum = decimal.MinValue;
        private string oper = "";
        private StringBuilder output;
        private bool firstOper = true;

        public Calculator()
        {
            InitializeComponent();
            outputTxt.Text = "0";
            output = new StringBuilder();
        }

        private void oneBtn_Click(object sender, EventArgs e)
        {
            output.Append("1");
            outputTxt.Text = output.ToString();
        }
        private void twoBtn_Click(object sender, EventArgs e)
        {
            output.Append("2");
            outputTxt.Text = output.ToString();
        }
        private void threeBtn_Click(object sender, EventArgs e)
        {
            output.Append("3");
            outputTxt.Text = output.ToString();
        }
        private void fourBtn_Click(object sender, EventArgs e)
        {
            output.Append("4");
            outputTxt.Text = output.ToString();
        }
        private void fiveBtn_Click(object sender, EventArgs e)
        {
            output.Append("5");
            outputTxt.Text = output.ToString();
        }
        private void sixBtn_Click(object sender, EventArgs e)
        {
            output.Append("6");
            outputTxt.Text = output.ToString();
        }
        private void sevenBtn_Click(object sender, EventArgs e)
        {
            output.Append("7");
            outputTxt.Text = output.ToString();
        }
        private void eightBtn_Click(object sender, EventArgs e)
        {
            output.Append("8");
            outputTxt.Text = output.ToString();
        }
        private void nineBtn_Click(object sender, EventArgs e)
        {
            output.Append("9");
            outputTxt.Text = output.ToString();
        }
        private void zeroBtn_Click(object sender, EventArgs e)
        {
            output.Append("0");
            outputTxt.Text = output.ToString();
        }
        private void dotBtn_Click(object sender, EventArgs e)
        {
            output.Append(".");
            outputTxt.Text = output.ToString();
        }

        private void plusBtn_Click(object sender, EventArgs e)
        {
            if (oper == "")
            {
                oper = "+";
                if (firstOper)
                {
                    firstNum = Convert.ToDecimal(output.ToString());
                    firstOper = false;
                }
                else
                {
                    secondNum = Convert.ToDecimal(output.ToString());
                    firstOper = true;
                }
                output.Clear();
            }
            else
            {
                secondNum = Convert.ToDecimal(output.ToString());
                firstOper = false;
                firstNum = Calc(oper, firstNum, secondNum);
                outputTxt.Text = firstNum.ToString();
                output.Clear();
                oper = "+";
            }
        }
        private void minusBtn_Click(object sender, EventArgs e)
        {
            if (oper == "")
            {
                oper = "-";
                if (firstOper)
                {
                    firstNum = Convert.ToDecimal(output.ToString());
                    firstOper = false;
                }
                else
                {
                    secondNum = Convert.ToDecimal(output.ToString());
                    firstOper = true;
                }
                output.Clear();
            }
            else
            {
                secondNum = Convert.ToDecimal(output.ToString());
                firstOper = false;
                firstNum = Calc(oper, firstNum, secondNum);
                outputTxt.Text = firstNum.ToString();
                output.Clear();
                oper = "-";
            }
        }
        private void multiplyBtn_Click(object sender, EventArgs e)
        {
            if (oper == "")
            {
                oper = "*";
                if (firstOper)
                {
                    firstNum = Convert.ToDecimal(output.ToString());
                    firstOper = false;
                }
                else
                {
                    secondNum = Convert.ToDecimal(output.ToString());
                    firstOper = true;
                }
                output.Clear();
            }
            else
            {
                secondNum = Convert.ToDecimal(output.ToString());
                firstOper = false;
                firstNum = Calc(oper, firstNum, secondNum);
                outputTxt.Text = firstNum.ToString();
                output.Clear();
                oper = "*";
            }
        }
        private void devideBtn_Click(object sender, EventArgs e)
        {
            if (oper == "")
            {
                oper = "/";
                if (firstOper)
                {
                    firstNum = Convert.ToDecimal(output.ToString());
                    firstOper = false;
                }
                else
                {
                    secondNum = Convert.ToDecimal(output.ToString());
                    firstOper = true;
                }
                output.Clear();
            }
            else
            {
                secondNum = Convert.ToDecimal(output.ToString());
                firstOper = false;
                firstNum = Calc(oper, firstNum, secondNum);
                outputTxt.Text = firstNum.ToString();
                output.Clear();
                oper = "/";
            }
        }

        private void equalBtn_Click(object sender, EventArgs e)
        {
            if (firstNum != decimal.MinValue)
            {
                secondNum = Convert.ToDecimal(output.ToString());
                firstOper = true;
                outputTxt.Text = Calc(oper, firstNum, secondNum).ToString();
            }
        }
        private decimal Calc(string oper, decimal first, decimal second)
        {
            decimal res = 0;
            switch (oper)
            {
                case "+":
                    res = first + second;
                    break;
                case "-":
                    res = first - second;
                    break;
                case "*":
                    res = first * second;
                    break;
                case "/":
                    res = first / second;
                    break;
                case "%":
                    res = first % second;
                    break;
                case "sqrt":
                    res = (decimal)Math.Sqrt(Convert.ToDouble(first));
                    break;
            }
            return res;
        }

        private void clearAllBtn_Click(object sender, EventArgs e)
        {
            output.Clear();
            oper = "";
            outputTxt.Text = "0";
        }
    }
}
